﻿Imports System.IO

Public Class FTPendaftaran

    'member variables
    Private _cSiswa As CSiswa

    'properties 
    Public Property IDSiswa As Integer

    'events 
    Public Event DataTersimpan()
    Public Event DataTerupdate()

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Text = My.Settings.NamaAplikasi & " [Pendaftaran Siswa Baru]"

        'set default tgl lahir
        tbTglLahir.Text = Now.Day
        tbBlnLahir.Text = Now.Month
        tbThnLahir.Text = Now.Year
    End Sub

    Public Sub New(ByVal idsiswa As Integer)
        Me.New()
        Me.IDSiswa = idsiswa
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCariFoto.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'set gambar di picture box
            Me.PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub


    Private Sub SimpanToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        'simpan data

        'validasi inputan
        Dim _isError As Boolean = False

        'validasi nama
        If tbNama.Text.Trim = "" Then
            'ganti warna background
            tbNama.BackColor = Color.Orange
            _isError = True
        End If

        'validasi nama ayah
        If tbNamaAyah.Text.Trim = "" Then
            'ganti warna background
            tbNamaAyah.BackColor = Color.Orange
            _isError = True
        End If

        'validasi nama ibu
        If tbNamaIbu.Text.Trim = "" Then
            'ganti warna background
            tbNamaIbu.BackColor = Color.Orange
            _isError = True
        End If

        'jika ada data kosong maka tampilkan pesan
        If _isError Then
            MessageBox.Show("Simpan gagal, data kosong.", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else

            Dim extFoto As String = Path.GetExtension(OpenFileDialog1.FileName)
            Dim namaFotoBaru As String = tbNama.Text.Trim.Replace(" ", "") & tbTglLahir.Text.Trim & tbBlnLahir.Text.Trim & tbThnLahir.Text.Trim & extFoto


            'simpan data ke dalam database
            '_cSiswa = New CSiswa
            '_cSiswa.simpan(Nothing, tbNama.Text.Trim, tbTempatLahir.Text.Trim, tbThnLahir.Text.Trim.Trim & "-" & tbBlnLahir.Text.Trim & "-" & tbTglLahir.Text.Trim, cbJK.Text, tbAlamat.Text.Trim, IIf(tbAnakke.Text.Trim = "", 0, tbAnakke.Text.Trim), IIf(tbJmlSaudara.Text.Trim = "", 0, tbJmlSaudara.Text.Trim), tbNamaAyah.Text.Trim, tbNamaIbu.Text.Trim, tbAlamatIbu.Text.Trim, tbTelpAyah.Text.Trim, tbTelpIbu.Text.Trim, 2, 2, tbNamaWali.Text.Trim, tbTelpWali.Text.Trim, tbAlamatWali.Text.Trim, namaFotoBaru)


            'copy foto ke folder foto
            MoveFiles(OpenFileDialog1.FileName, My.Settings.DirektoriFoto, namaFotoBaru)

            RaiseEvent DataTersimpan()
            ResetControl()
            'set control focus
            tbNama.Focus()
        End If
    End Sub

    Public Sub MoveFiles(ByVal sourcePath As String, ByVal DestinationPath As String, ByVal filename As String)
        If File.Exists(sourcePath) Then
            Dim dFilePath As String = String.Empty
            dFilePath = DestinationPath + "\" + filename
            'File.Move(sourcePath, dFilePath)
            'MsgBox(sourcePath & vbTab & DestinationPath & vbTab & dFilePath)

            File.Copy(sourcePath, dFilePath, True)
        End If


    End Sub

    Private Sub ResetControl()
        'clear data
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox Then
                CType(ctl, TextBox).Clear()
            End If
        Next
        PictureBox1.Image = My.Resources.faceicon
        'set backcolor
        tbNama.BackColor = Me.tbAlamat.BackColor
        tbNamaIbu.BackColor = Me.tbAlamat.BackColor
        tbNamaAyah.BackColor = Me.tbAlamat.BackColor
    End Sub

    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        ResetControl()
    End Sub

    Private Sub FTPendaftaran_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    ''' <summary>
    ''' menampikan form untuk edit data
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub showEdit()
        showDetil()
        Me.Text = My.Settings.NamaAplikasi & " [Rubah Siswa]"
        SimpanToolStripMenuItem.Visible = False
        ResetToolStripMenuItem.Visible = True
        EditToolStripMenuItem.Visible = False
        SimpanRubahToolStripMenuItem.Visible = True
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox Then
                CType(ctl, TextBox).ReadOnly = False
                CType(ctl, TextBox).BackColor = Color.Silver
            End If
        Next
        cbJK.Enabled = True
        Me.Show()
    End Sub

    ''' <summary>
    ''' menampilkan form untuk detil data, semua control read only
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub showDetil()
        Me.Text = My.Settings.NamaAplikasi & " [Detil Siswa]"
        SimpanToolStripMenuItem.Visible = False
        ResetToolStripMenuItem.Visible = False
        EditToolStripMenuItem.Visible = True
        SimpanRubahToolStripMenuItem.Visible = False
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox Then
                CType(ctl, TextBox).ReadOnly = True
                CType(ctl, TextBox).BackColor = Color.Silver
            End If
        Next
        cbJK.Enabled = False

        'tampilkan data ke control
        _cSiswa = New CSiswa
        Dim _dTab As DataTable = _cSiswa.getById(Me.IDSiswa)
        With _dTab(0)
            tbNama.Text = .Item("nama")
            tbTempatLahir.Text = .Item("tmpt_lhr")
            tbTglLahir.Text = CDate(.Item("tgl_lhr")).Day
            tbBlnLahir.Text = CDate(.Item("tgl_lhr")).Month
            tbThnLahir.Text = CDate(.Item("tgl_lhr")).Year
            cbJK.SelectedItem = .Item("jk").ToString
            tbAlamat.Text = .Item("almt").ToString
            tbAnakke.Text = .Item("ank_ke").ToString
            tbJmlSaudara.Text = .Item("jml_sdr").ToString
            tbNamaAyah.Text = .Item("nm_ayh").ToString
            tbNamaIbu.Text = .Item("nm_ibu").ToString
            tbPekerjaanAyah.Text = .Item("tmpt_lhr").ToString
            tbPekerjaanIbu.Text = .Item("tmpt_lhr").ToString
            tbTelpAyah.Text = .Item("telp_ayh").ToString
            tbTelpIbu.Text = .Item("telp_ibu").ToString
            tbAlamatIbu.Text = .Item("almt_ortu").ToString
            tbNamaWali.Text = .Item("nm_wli").ToString
            tbTelpWali.Text = .Item("tlp_wli").ToString
            tbAlamatWali.Text = .Item("almt_wli").ToString
            tbNISN.Text = .Item("nisn").ToString
            Try
                PictureBox1.Image = Image.FromFile(My.Settings.DirektoriFoto & "\" & .Item("foto"))
            Catch ex As FileNotFoundException
                PictureBox1.Image = My.Resources.faceicon
            End Try

        End With


        Me.Show()
    End Sub



    Private Sub SimpanRubahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanRubahToolStripMenuItem.Click
        'simpan data perubahan
        'simpan data

        'validasi inputan
        Dim _isError As Boolean = False

        'validasi nama
        If tbNama.Text.Trim = "" Then
            'ganti warna background
            tbNama.BackColor = Color.Orange
            _isError = True
        End If

        'validasi nama ayah
        If tbNamaAyah.Text.Trim = "" Then
            'ganti warna background
            tbNamaAyah.BackColor = Color.Orange
            _isError = True
        End If

        'validasi nama ibu
        If tbNamaIbu.Text.Trim = "" Then
            'ganti warna background
            tbNamaIbu.BackColor = Color.Orange
            _isError = True
        End If

        'jika ada data kosong maka tampilkan pesan
        If _isError Then
            MessageBox.Show("Simpan gagal, data kosong.", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            'set nanma foto
            Dim extFoto As String = Path.GetExtension(OpenFileDialog1.FileName)
            Dim namaFotoBaru As String = tbNama.Text.Trim.Replace(" ", "") & tbTglLahir.Text.Trim & tbBlnLahir.Text.Trim & tbThnLahir.Text.Trim & extFoto

            'simpan data ke dalam database
            _cSiswa = New CSiswa
            _cSiswa.rubah(Me.IDSiswa, tbNISN.Text.Trim, tbNama.Text.Trim, tbTempatLahir.Text.Trim, tbThnLahir.Text.Trim.Trim & "-" & tbBlnLahir.Text.Trim & "-" & tbTglLahir.Text.Trim, cbJK.Text, tbAlamat.Text.Trim, IIf(tbAnakke.Text.Trim = "", 0, tbAnakke.Text.Trim), IIf(tbJmlSaudara.Text.Trim = "", 0, tbJmlSaudara.Text.Trim), tbNamaAyah.Text.Trim, tbNamaIbu.Text.Trim, tbAlamatIbu.Text.Trim, tbTelpAyah.Text.Trim, tbTelpIbu.Text.Trim, 2, 2, tbNamaWali.Text.Trim, tbTelpWali.Text.Trim, tbAlamatWali.Text.Trim, namaFotoBaru)

            'replace foto
            If OpenFileDialog1.FileName <> "" Then
                'replace

                MoveFiles(OpenFileDialog1.FileName, My.Settings.DirektoriFoto, namaFotoBaru)
            End If

            RaiseEvent DataTerupdate()
            showDetil()
        End If
    End Sub

    Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click
        showEdit()
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpan.Click
        SimpanToolStripMenuItem.PerformClick()
    End Sub

    Private Sub btnPerubahan_Click(sender As System.Object, e As System.EventArgs) Handles btnPerubahan.Click
        SimpanRubahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub btnRubah_Click(sender As System.Object, e As System.EventArgs) Handles btnRubah.Click
        EditToolStripMenuItem.PerformClick()
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        ResetToolStripMenuItem.PerformClick()
    End Sub

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub

    Private Sub SimpanToolStripMenuItem_VisibleChanged(sender As Object, e As System.EventArgs) Handles SimpanToolStripMenuItem.VisibleChanged
        btnSimpan.Visible = SimpanToolStripMenuItem.Visible
    End Sub

    Private Sub SimpanRubahToolStripMenuItem_VisibleChanged(sender As Object, e As System.EventArgs) Handles SimpanRubahToolStripMenuItem.VisibleChanged
        btnPerubahan.Visible = SimpanRubahToolStripMenuItem.Visible
    End Sub

    Private Sub EditToolStripMenuItem_VisibleChanged(sender As Object, e As System.EventArgs) Handles EditToolStripMenuItem.VisibleChanged
        btnRubah.Visible = EditToolStripMenuItem.Visible
    End Sub

    Private Sub ResetToolStripMenuItem_VisibleChanged(sender As Object, e As System.EventArgs) Handles ResetToolStripMenuItem.VisibleChanged
        btnReset.Visible = ResetToolStripMenuItem.Visible
    End Sub

    Private Sub KeluarToolStripMenuItem_VisibleChanged(sender As Object, e As System.EventArgs) Handles KeluarToolStripMenuItem.VisibleChanged
        btnKeluar.Visible = KeluarToolStripMenuItem.Visible
    End Sub
End Class